﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Net.Chirash.Twitter.TweetZone.WindowComponents
{
    /// <summary>
    /// RefreshIndicator.xaml の相互作用ロジック
    /// </summary>
    public partial class RefreshIndicator : UserControl
    {
        #region Constractor

        public RefreshIndicator()
        {
            this.InitializeComponent();

            AddHandler(IsRefreshingEvent, new RoutedPropertyChangedEventHandler<bool>(AnimatnionControlEvent));
        }

        #endregion

        #region DependencyProperties

        public static readonly DependencyProperty IsRefreshingDependencyProperty
           = DependencyProperty.Register("IsRefreshing", typeof(bool), typeof(RefreshIndicator)
                , new FrameworkPropertyMetadata(new PropertyChangedCallback(OnIsRefreshingChanged)));

        #endregion

        #region RoutedEvents

        public static readonly RoutedEvent IsRefreshingEvent
            = EventManager.RegisterRoutedEvent("IsRefreshingChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<bool>), typeof(RefreshIndicator));

        public event RoutedEventHandler IsRefreshingChanged
        {
            add { base.AddHandler(IsRefreshingEvent, value); }
            remove { base.RemoveHandler(IsRefreshingEvent, value); }
        }

        private static void OnIsRefreshingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var routedArgs = new RoutedPropertyChangedEventArgs<bool>((bool)e.OldValue, (bool)e.NewValue, IsRefreshingEvent);
            (d as RefreshIndicator).OnIsRefreshingChanged(routedArgs);
        }

        protected virtual void OnIsRefreshingChanged(RoutedPropertyChangedEventArgs<bool> e)
        {
            base.RaiseEvent(e);
        }

        #endregion

        #region Public Property

        public bool IsRefreshing
        {
            get { return (bool)GetValue(IsRefreshingDependencyProperty); }
            set { SetValue(IsRefreshingDependencyProperty, value); }
        }

        #endregion

        #region Private Methods

        private void StartAnimation()
        {
            rotation.Stop();
            rotation.Begin();
        }

        private void StopAnimation()
        {
            rotation.Stop();
        }

        private void AnimatnionControlEvent(Object sender, RoutedPropertyChangedEventArgs<bool> e)
        {
            if (e.NewValue)
            {
                StartAnimation();
            }
            else
            {
                StopAnimation();
            }
        }

        #endregion

    }
}